//package ru.tvsamara.staff.Security.Token;
//
//import com.fasterxml.jackson.databind.ObjectMapper;
//import jakarta.servlet.FilterChain;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.springframework.http.HttpMethod;
//import org.springframework.http.MediaType;
//import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
//import org.springframework.security.web.csrf.CsrfTokenRepository;
//import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
//import org.springframework.security.web.util.matcher.RequestMatcher;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import java.io.IOException;
//
//public class GetCsrfTokenFilter extends OncePerRequestFilter {
//
//    private RequestMatcher requestMatcher = new AntPathRequestMatcher("/csrf", HttpMethod.GET.name());
//
//    private CsrfTokenRepository csrfTokenRepository = new CookieCsrfTokenRepository();
//
//    private ObjectMapper objectMapper = new ObjectMapper();
//
//    public void setRequestMatcher(RequestMatcher requestMatcher) {
//        this.requestMatcher = requestMatcher;
//    }
//
//    public void setCsrfTokenRepository(CsrfTokenRepository csrfTokenRepository) {
//        this.csrfTokenRepository = csrfTokenRepository;
//    }
//
//    public void setObjectMapper(ObjectMapper objectMapper) {
//        this.objectMapper = objectMapper;
//    }
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        if (this.requestMatcher.matches(request)) {
//            response.setStatus(HttpServletResponse.SC_OK);
//            response.setContentType(MediaType.APPLICATION_JSON_VALUE);
//            this.objectMapper.writeValue(response.getWriter(), this.csrfTokenRepository.loadDeferredToken(request, response).get());
//            return;
//        }
//
//        filterChain.doFilter(request, response);
//    }
//}
